<?php 
class Application_Model_Task extends Application_Model_DomainObject {
    
    protected $_id;
    
    protected $_name; //任务内容
    
    protected $_type; //任务类型

    protected $_score; //经验值
    
    protected $_dateCreated;//创建时间
    
    protected $_status;//状态
    
    protected $_operator;//添加人
    
    protected $_code;//编码
    /**
     * 新手任务
     */
    const Type_Novice = 1;
    /**
     * 日常任务
     */
    const Type_Daily = 2;
    /**
     * 可用
     */
    const Status_Launch = 1;
    /**
     * 禁用
     */
    const Status_Disable = 2;
    /**
     * 每天第一次登录悠奇 id 为 1
     */
    const TASK_LOGIN = "login";
    /**
     * 验证邮箱
     */
    const TASK_EMAIL = "email";
    /**
     * 上传头像
     */
    const TASK_PIC = "pic";
    /**
     * 完善个人资料
     */
    const TASK_USERINFO = "userinfo";
    /**
     * 第一次代购
     */
    const TASK_ORDER = "order";
    /**
     * 第一次充值
     */
    const TASK_RECHARGE = "recharge";
    /**
     * 第一次国际运单
     */
    const TASK_WAYBILL = "waybill";
    /**
     * 关注微博
     */
    const TASK_WEIBO = "weibo";
    /**
     * 关注facebook
     */
    const TASK_FACEBOOK = "facebook";
    /**
     * 第一件商品入库
     */
    const TASK_INSTORE = "instore";
    /**
     * 关注微信
     */
    const TASK_WEIXIN = "weixin";
    /**
     * 第一次上门取件
     */
    const TASK_VISITING = "visiting";
    /**
     * 会员礼物发国际运单
     */
    const TASK_GIFTWAYBILL = "giftwaybill";

    public static function getTypeAll()
    {
        return array(
            self::Type_Novice => "新手任务"
            , self::Type_Daily => "日常任务"
        );
    }
    
    public function getTypeName()
    {
        $arrType = self::getTypeAll();
        return $arrType[$this->_type];
    }
    
    public static function getStatusAll()
    {
        return array(
            self::Status_Launch => "启用"
            , self::Status_Disable => "禁用"
        );
    }
    
    public function getStatusName()
    {
        $arrStatus = self::getStatusAll();
        return $arrStatus[$this->_status];
    }
    
    /**
     * 完成人数
     */
    public function getCompleteNum()
    {
        return Application_Model_TaskComplete::getCompleteNumByTaskId($this->_id);
    }
    
    public static function getTaskFileds($taskSearch,$pagesize,$pageindex)
    {
        return self::getDao()->queryPage($taskSearch->getSearchFileds(), " id asc ", $pagesize, $pageindex);
    }
    
    public static function addOrEditTask($id,$name,$type,$score,$status,$code)
    {
        $pd = true;
        if($id != ""){
            $taskItem = self::load($id);
            if(!$taskItem){
                $taskItem = new Application_Model_Task();
                $taskItem->DateCreated = date("Y-m-d H:i:s");
                $taskItem->Operator = Uuch_Util::getUserNikeName();
                $pd = false;
            }
        } else {
            $taskItem = new Application_Model_Task();
            $taskItem->DateCreated = date("Y-m-d H:i:s");
            $taskItem->Operator = Uuch_Util::getUserNikeName();
            $pd = false;
        }
        $taskItem->Code = $code;
        $taskItem->Name = $name;
        $taskItem->Type = $type;
        $taskItem->Score = $score;
        $taskItem->Status = $status;
        $taskItem->save();
        Application_Model_Operate::addOperate(0, null, $taskItem->Id, ($pd?"修改用户任务:":"添加用户任务:").$name, 0);
        return $taskItem->Id;
    }
    
    public function addTaskLog($userId)
    {
        $taskComplete = new Application_Model_TaskComplete();
        $taskComplete->TaskId = $this->_id;
        $taskComplete->UserId = $userId;
        $taskComplete->DateCreated = date("Y-m-d H:i:s");
        $taskComplete->save();
    }
    
    public static function getByTaskCode($taskCode)
    {
        return self::getDao()->queryOne(array("code = "=>$taskCode));
    }
    
    public static function getTaskComplete($taskCode,$userId)
    {
        $taskItem = self::getByTaskCode($taskCode);
        if($taskItem){
            $completeItem = Application_Model_TaskComplete::getByTaskIdUserId($taskItem->Id,$userId);
            return $completeItem;
        }else{
            return false;
        }
    }
}